<template>
  <div class="container">
    <transition :enter-active-class="proxy?.animate.searchAnimate.enter" :leave-active-class="proxy?.animate.searchAnimate.leave">
      <div v-show="showSearch" class="mb-[10px]">
        <el-card shadow="hover">
          <el-form ref="queryFormRef" :model="queryParams" :inline="true">
            <el-form-item label="预案名称" prop="monitorPlanName">
              <el-input v-model="queryParams.monitorPlanName" placeholder="请输入监控预案名称" clearable @keyup.enter="handleQuery" />
            </el-form-item>
            <el-form-item label="更新时间" prop="">
              <el-date-picker
                v-model="startAndEndTime"
                type="daterange"
                start-placeholder="开始日期"
                end-placeholder="结束日期"
                value-format="YYYY-MM-DD"
                clearable
                @change="handleDateChange"
              />
            </el-form-item>
            <el-form-item>
              <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
              <el-button icon="Refresh" @click="resetQuery">重置</el-button>
            </el-form-item>
          </el-form>
        </el-card>
      </div>
    </transition>

    <el-card shadow="never" style="flex: 1; display: flex; flex-direction: column; position: relative">
      <template #header>
        <el-row :gutter="10" class="mb8">
          <el-col :span="1.5">
            <el-button v-hasPermi="['camera:camera-monitor-plan:add']" type="primary" plain icon="Plus" @click="handleAdd">新增 </el-button>
          </el-col>
          <el-col :span="1.5">
            <el-button v-hasPermi="['camera:camera-monitor-plan:edit']" type="success" plain icon="Edit" :disabled="single" @click="handleUpdate()">
              修改
            </el-button>
          </el-col>
          <el-col :span="1.5">
            <el-button v-hasPermi="['camera:camera-monitor-plan:remove']" type="danger" plain icon="Delete" :disabled="multiple" @click="handleDelete()" >删除 </el-button>
          </el-col>
          <right-toolbar v-model:showSearch="showSearch" @query-table="getList"></right-toolbar>
        </el-row>
      </template>

      <el-table v-loading="loading" :data="cameraMonitorPlanList" style="flex: 1;" @selection-change="handleSelectionChange">
        <el-table-column type="selection" width="55" align="center" />
        <el-table-column v-if="false" label="主键" align="center" prop="cameraMonitorPlanId" />
        <el-table-column label="预案名称" align="center" prop="monitorPlanName" />
        <el-table-column label="监控数量" align="center" prop="equipmentCount" />
        <el-table-column label="是否轮播" align="center" prop="whetherCarousel" >
            <template #default="scope">
            <dict-tag :options="whether_carousel" :value="scope.row.whetherCarousel"/>
          </template>
        </el-table-column>
        <el-table-column label="轮播时间(秒)" align="center" prop="carouselTime" min-width="110" />
        <el-table-column label="更新时间" align="center" prop="updateTime" min-width="180" show-overflow-tooltip />
        <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
          <template #default="scope">
            <el-button v-hasPermi="['camera:camera-monitor-plan:edit']" link type="primary" @click="handleUpdate(scope.row)" >修改</el-button>
            <el-button v-hasPermi="['camera:camera-monitor-plan:remove']" link type="primary" @click="handleDelete(scope.row)" >删除</el-button>
          </template>
        </el-table-column>
      </el-table>

      <pagination v-model:page="queryParams.pageNum" v-model:limit="queryParams.pageSize" :total="total" @pagination="getList" />
    </el-card>
    <!-- 添加监控预案对话框 -->
    <el-drawer v-model="dialog.shouAdd" :title="dialog.title" append-to-body destroy-on-close size="800px">
      <add @close="dialog.shouAdd = false" @success="getList()"/>
    </el-drawer>
    <!-- 修改监控预案对话框 -->
    <el-drawer v-model="dialog.showUpdate" :title="dialog.title" append-to-body destroy-on-close size="800px">
      <update :camera-monitor-plan-id="cameraMonitorPlanId" @close="dialog.showUpdate = false" @success="getList()" />
    </el-drawer>
  </div>
</template>

<script setup name="cameraMonitorPlan" lang="ts">
import { delcameraMonitorPlan, listcameraMonitorPlan } from '@/api/security/video-monitor/camera-monitor-plan';
import { cameraMonitorPlanForm, cameraMonitorPlanQuery, cameraMonitorPlanVO } from '@/api/security/video-monitor/camera-monitor-plan/types';
import add from './components/add.vue';
import update from './components/update.vue';

const { proxy } = getCurrentInstance() as ComponentInternalInstance;
const { whether_carousel } = toRefs<any>(proxy?.useDict('whether_carousel'));

const cameraMonitorPlanList = ref<cameraMonitorPlanVO[]>([]);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref<Array<string | number>>([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);
const queryFormRef = ref<ElFormInstance>();

const dialog = reactive<DialogOption>({
  visible: false,
  title: ''
});

const data = reactive({
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    monitorPlanName: undefined,
    carouselTime: undefined,
    startTime: undefined,
    endTime: undefined,
    params: {}
  }
});

const { queryParams } = toRefs(data);

/** 查询监控预案列表 */
const getList = async () => {
  loading.value = true;
  const res = await listcameraMonitorPlan(queryParams.value);
  cameraMonitorPlanList.value = res.rows;
  total.value = res.total;
  loading.value = false;
};

/** 搜索按钮操作 */
const handleQuery = () => {
  queryParams.value.pageNum = 1;
  getList();
};

/** 重置按钮操作 */
const resetQuery = () => {
  queryFormRef.value?.resetFields();
  startAndEndTime.value = [];
  queryParams.value.startTime = undefined;
  queryParams.value.endTime = undefined; // 手动清空日期选择器的值
  handleQuery();
};

/** 多选框选中数据 */
const handleSelectionChange = (selection: cameraMonitorPlanVO[]) => {
  ids.value = selection.map((item) => item.cameraMonitorPlanId);
  single.value = selection.length != 1;
  multiple.value = !selection.length;
};

/** 新增按钮操作 */
const handleAdd = () => {
  dialog.shouAdd = true;
  dialog.title = '预案场景配置';
};

/** 修改按钮操作 */
const cameraMonitorPlanId = ref();
const handleUpdate = async (row?: cameraMonitorPlanVO) => {
  cameraMonitorPlanId.value = row?.cameraMonitorPlanId || ids.value[0];

  dialog.showUpdate = true;
  dialog.title = '修改监控预案';
};

/** 删除按钮操作 */
const handleDelete = async (row?: cameraMonitorPlanVO) => {
  const _cameraMonitorPlanIds = row?.cameraMonitorPlanId || ids.value;
  await proxy?.$modal.confirm('是否确认删除监控预案编号为"' + _cameraMonitorPlanIds + '"的数据项？').finally(() => (loading.value = false));
  await delcameraMonitorPlan(_cameraMonitorPlanIds);
  proxy?.$modal.msgSuccess('删除成功');
  await getList();
};

/** 导出按钮操作 */
const handleExport = () => {
  proxy?.download(
    'camera/camera-monitor-plan/export',
    {
      ...queryParams.value
    },
    `camera-monitor-plan_${new Date().getTime()}.xlsx`
  );
};

/** 日期框改变 */
const startAndEndTime = ref([])
const handleDateChange = () => {
  if (startAndEndTime.value && startAndEndTime.value.length === 2) {
    queryParams.value.startTime = startAndEndTime.value[0] + ' 00:00:00';
    queryParams.value.endTime = startAndEndTime.value[1] + ' 23:59:59';
  } else {
    queryParams.value.startTime = undefined;
    queryParams.value.endTime = undefined;
  }

  console.log('queryParams.value', queryParams.value)
}

onMounted(() => {
  getList();
});
</script>
<style lang="scss" scoped>
:deep(.el-card__header) {
  height: 60px;
}

:deep(.el-card__body) {
  height: calc(100% - 60px);
  display: flex;
  flex-direction: column;
}

:deep(.pagination-container) {
  margin-top: 0;
}
</style>
